<?php
defined('BASEPATH') or exit('No direct script access allowed');

/**
 * TP_Controller class
 *
 * @Name 根控制器
 * @Desc 方法描述
 * @Author Pekey
 * @Date 2020-08-13
 */
class TP_Controller extends CI_Controller
{
    //未被过滤的post
    protected $ORI_POST = false;
    //未被过滤的get
    protected $ORI_GET = false;

    //构造函数
    protected function __construct()
    {
        parent::__construct();
        setHeader(); //处理跨域问题

        //$this->checkOnline();//判断是否上线中

        //如果是options的预请求，直接返回200退出
        if ($_SERVER['REQUEST_METHOD'] == "OPTIONS") {
            header("HTTP/1.0 200 OK");
            exit();
        }

        getHeaders(); //获取相关标头数据

        $this->load->driver('cache');
        //XSS过滤
        $this->xssEscape();
        //接口调用次数限制
        $this->ask_count();
        //解析参数
        $this->initComment(true);
    }

    /**
     * xssEscape function
     *
     * @Name XSS过滤
     * @Desc XSS过滤
     * @OpenFlag -1
     * @Author Pekey
     * @Date 2020-08-13
     * @return void
     */
    private function xssEscape()
    {
        if (isset($_POST)) {
            $this->ORI_POST = $_POST;
            // $_POST          = $this->input->post();
            //通过application/json方式与前端传递
            $data = object_to_array(json_decode(isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input")));
            $_POST = array_merge($_POST,$data);
          
        }
    }

    /**
     * ask_count function
     *
     * @Name 请求接口次数
     * @Desc 限制请求接口次数
     * @OpenFlag -1
     * @Author Pekey
     * @Date 2020-08-13
     * @return void
     */
    private function ask_count()
    {
        $ask_times_redis = array();

        $ask_times_redis['ask_url'] = sys_GetCurUrl();
        $ask_times_redis['clien_ip'] = sys_get_client_ip();
        //有效时间内,单位:秒
        $ip_url_num = count($this->cache->redis->keys($ask_times_redis));
        if ($ip_url_num >= API_ASK_LIMIT_NUM) {
            throw new \TPException(ErrCode::ErrCode_10105);
        }
        //执行插入数组redis
        // $ask_times_redis[FLAG] = date('Y-m-d H:i:s', time()); //标志性区分
        $this->cache->redis->set($ask_times_redis, date('Y-m-d H:i:s', time()), API_ASK_TIME);
    }

    /**
     * initComment function
     *
     * @Name 初始化注释
     * @Desc 注释内容解析成日志、接口权限等
     * @OpenFlag -1
     * @Author Pekey
     * @Date 2020-08-13
     * @return void
     */
    protected function initComment($isAdmin = false)
    {
        //获取当前控制器
        $con = $this->router->fetch_class();
        //获取当前方法
        $func = $this->router->fetch_method();
        //通过反映射获取注释信息
        $reflection = new ReflectionClass($con);
        $method = $reflection->getMethod($func);
        $comment = $method->getDocComment();

        //执行接口权限
        if ($isAdmin) {
            $this->checkMethodAuth();
        }

        //执行日志记录
        // $this->initLogger($con . '/' . $func, $comment, $isAdmin ? $_SESSION[SESS_HEADER]['uuid'] : "");

    }

    private function checkMethodAuth()
    {

    }

    /**
     * initLogger function
     *
     * @Name 日志记录
     * @Desc 方法描述
     * @OpenFlag -1
     * @Author Pekey
     * @Date 2020-08-13
     * @param [type] $actionname
     * @param [type] $desc
     * @param [type] $uuid
     * @return void
     */
    private function initLogger($actionname, $desc, $uuid)
    {
        $this->load->Service('PService/LoggerService');
        $this->LoggerService->saveLogger($actionname, $desc, $uuid);
    }
    /**
     * doTransaction function
     *
     * @Name 事务方法
     * @Desc 处理事务问题
     * @OpenFlag -1`
     * @Author Pekey
     * @Date 2020-08-13
     * @param [type] $func
     * @return void
     */
    protected function doTransaction($func)
    {
        $this->db->trans_begin();
        try {
            $args = func_get_args();
            unset($args[0]);
            $nums = func_num_args();
            call_user_func_array($func, $args);
        } catch (Exception $e) {
            throw new \TPException(ErrCode::ErrCode_10001);
        } catch (Error $e) {
            throw new \TPException(ErrCode::ErrCode_10001);
        } finally {
            if ($this->db->trans_status() === false) {
                $this->db->trans_rollback();
            } else {
                $this->db->trans_commit();
            }
        }
    }

    /**
     * checkOnline function
     * 
     * @Name checkOnline
     * @Desc 判断是否上线中
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      LiAnXu
     * @Date 2020-11-30 13:29:02 星期一
     * @return      [type]                   [description]
     */
    private function checkOnline(){
        $this->load->service('PService/DicService');
        $data=$this->DicService->getConfigs(DicKey::ONLINE[TPEnum::Caption],DicType::SYSTEMINFO[TPEnum::Value]);
        if($data[0]['dic_value']==EnableState::Enable[TPEnum::Value]){
            exit(retJson(ErrCode::ErrCode_10406));
        }
    }
}

/**
 * Comm_Controller class
 *
 * @Name Comm控制器
 * @Desc 普通API接口，不做普通接口token，仅做接口次数限制
 * @Author Pekey
 * @Date 2020-08-13
 */
class Comm_Controller extends TP_Controller
{
    //构造函数
    protected function __construct()
    {
        parent::__construct();
        $this->initComment();
    }
}

/**
 *
 */
/**
 * Admin_Controller class
 *
 * @Name admin控制器
 * @Desc 带Admin管理端API接口控制
 * @OpenFlag 是否开放,1开放0权限控制
 * @Transaction 是否开启事务,1开启0关闭
 * @Author Pekey
 * @Date 2020-08-13
 */
class Admin_Controller extends TP_Controller
{
    //构造函数
    protected function __construct()
    {
        parent::__construct();
        $this->load->Service('PService/SecurityService');
        $this->load->service("PService/AccountService");
        $this->SecurityService->checkTokens(); //验证token
    }
}

/**
 * Home_Controller class
 *
 * @Name User控制器
 * @Desc 带移动端用户端API接口控制
 * @Author Pekey
 * @Date 2020-08-13
 */
class Home_Controller extends TP_Controller
{
    //构造函数
    protected function __construct()
    {
        parent::__construct();

    }
}
